<?php

require_once ROOT_DIR.'models/Message.php';
require_once ROOT_DIR.'views/MessageJSON.php';
require_once ROOT_DIR.'controls/RightHelper.php';

/**
 * Tries to retrieve the message stored under the given id an echos the result
 * @param int $id
 * @param boolean $fullResponse
 */
function getMsg($id, $fullResponse = false, $responseOrder = 'asc'){
	
	if(is_null($fullResponse)) $fullResponse = false;
	if(is_null($responseOrder)) $responseOrder = 'asc';
	
	$msg = Message::getMessage($id);
	if(is_null($msg)){
		$res = new ResultJSON(true, session_id(), ERROR_MSG_DOES_NOT_EXIST);
		echo $res->renderView();
	} else {
		if($_SESSION['USER_NAME'] == 'admin' || $msg->getAuthorID() == $_SESSION['USER_ID'] || RightHelper::checkIfThreadVisible($msg)){
			echo MessageJSON::getView($msg, $fullResponse, null, $responseOrder);
		} else {
			$res = new ResultJSON(true, session_id(), ERROR_NOT_ALLOWED);
			echo $res->renderView();
		}
	}
}

?>